import java.util.*;
import java.io.*;

public class demo2{
    public static void main(String[] args) throws Throwable{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        String[] str = reader.readLine().split(" ");
        String s1 = str[0], s2 = str[1];
        int prev1 = -1, prev2 = -1, ret = 0x3f3f3f3f;

        for(int i = 0; i < n; i++){
            String s = reader.readLine();
            if(s.equals(s1)){
                if(prev2 != -1){
                    ret = Math.min(ret, i - prev2);
                }
                prev1 = i;
            }

            else if(s.equals(s1)){
                if(prev1 != -1){
                    ret = Math.min(ret, i - prev1);
                }
                prev2 = i;
            }
        }

        System.out.println(ret == 0x3f3f3f3f ? -1 : ret);
    }
}